﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mischief;

namespace KazooUI
{
    public partial class Form5 : Form
    {
        #region LocxalGlobals
        #region delagtes
        delegate void DelThreadUpdateFormInt(int i);
        DelThreadUpdateFormInt notifyiconswitch;
        DelThreadUpdateFormInt progressbarlevel;
        DelThreadUpdateFormInt ListenerCount;
        DelThreadUpdateFormInt progressbarcolor;

        delegate void DelThreadUpdateFormString(string user);
        DelThreadUpdateFormString addusertolist;
        DelThreadUpdateFormString removeuserfromlist;
        DelThreadUpdateFormString currentuserkeyed;
        DelThreadUpdateFormString checkuserlist;

        delegate void DelThreadUpdateBoolItemSwitch(object t, bool bstate);
        DelThreadUpdateBoolItemSwitch BoolItemSwitch;
        #endregion
        YMVCHook.PacketHook p;
        VoiceStream wd;
        System.Collections.ArrayList IgnoreList;
        #endregion

        #region constructor
        public Form5()
        {
            InitializeComponent();
            checkuserlist = new DelThreadUpdateFormString(listviewitemcheck);
            BoolItemSwitch = new DelThreadUpdateBoolItemSwitch(ContextMenuItemShow);
            currentuserkeyed = new DelThreadUpdateFormString(progressbarcurrentkeyeduser);
            addusertolist = new DelThreadUpdateFormString(listviewadduser);
            removeuserfromlist = new DelThreadUpdateFormString(listviewdeluser);
            notifyiconswitch = new DelThreadUpdateFormInt(switchto);
            ListenerCount = new DelThreadUpdateFormInt(button1chagetext);
            progressbarlevel = new DelThreadUpdateFormInt(progressbarchangelevel);
            progressbarcolor = new DelThreadUpdateFormInt(progressbarchangecolor);
            p = new YMVCHook.PacketHook();
            p.HookedRoomEvent += new YMVCHook.PacketHook.HookedRoomInfo(p_HookedEvent);
            p.HookedUserNameEvent += new YMVCHook.PacketHook.HookedUserInfo(p_HookedUserNameEvent);
            p.username = "";
            p.start();
            
        }
        #endregion

        #region delegate methods
        void ContextMenuItemShow(object t, bool state)
        {
            try
            {
                ToolStripMenuItem t1 = (ToolStripMenuItem)t;
                t1.Visible = state;
            }
            catch
            {
                try
                {
                    PureComponents.NicePanel.NicePanel t1 = (PureComponents.NicePanel.NicePanel)t;
                    t1.Visible = state;
                }
                catch
                {
                    Timer t1 = (Timer)t;
                    t1.Enabled = state;
                }
            }


        }
        void progressbarchangecolor(int i)
        {
            if (i == 0)
                xtraProgressBar1.BarColor1 = Color.MistyRose;
            else
                xtraProgressBar1.BarColor1 = Color.WhiteSmoke;
        }
        private void listviewitemcheck(string data)
        {
            listView1.Items[data].Checked = !listView1.Items[data].Checked;
        }
        private void listviewadduser(string data)
        {
            listView1.Items.Add(data, data, 0);
            if(IgnoreList.Contains(data))
            {
               new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ignoreshort)).Start(data);
            }
        }
        private void progressbarchangelevel(int i)
        {
            xtraProgressBar1.BarValue = i;
        }
        private void listviewdeluser(string data)
        {
            listView1.Items.RemoveByKey(data);
        }
        private void progressbarcurrentkeyeduser(string data)
        {
            xtraProgressBar1.TextBegin = data + " ";
        }
        private void button1chagetext(int i)
        {
            button1.Text = Convert.ToString(i); ;
        }
        #endregion

        #region PacketHookEvent Handlers
        void p_HookedUserNameEvent(string username)
        {
            Invoke(notifyiconswitch, new object[] { 1 });
        }

        void p_HookedEvent(string username, string roomname, string roomspace, string roomkey)
        {
            if (username != "" && roomname != "" && roomspace != "" && roomkey != "")
            {
                Invoke(notifyiconswitch, new object[] { 2 });
                wd = new VoiceStream(username, roomname, roomspace, roomkey, System.Net.Dns.Resolve("vc.yahoo.com").AddressList[0].ToString());
                wd.VCUserJoin += new VoiceStream.VCEventHandler3(wd_AddUser);
                wd.VCUserLeave += new VoiceStream.VCEventHandler3(wd_DelUser);
                wd.KeyUser += new VoiceStream.KazooListHandler(wd_KeyUser);
                wd.LevelUser += new VoiceStream.KazooListHandler1(wd_LevelUser);
                wd.Listners1 += new VoiceStream.VCEventHandler3(wd_Listners1);
                wd.VCConnected += new VoiceStream.VCEventHandler3(wd_VCConnected);
                if (autojoinStripMenuItem1.Checked)
                    JoinVc();
            }
            else
            {
                Invoke(notifyiconswitch, new object[] { 1 });
                wd = null;
            }
        }
        #endregion

        #region Notifyiconstates
        void switchto(int i)
        {
            if (i == 0)
            {
                this.notifyIcon1.Icon = new Icon("a2.ico");
                notifyIcon1.ShowBalloonTip(5, "Awaiting", "User, Connect To Chat Now", ToolTipIcon.Info);
            }
            if (i == 1)
            {
                this.notifyIcon1.Icon = new Icon("a.ico");
                notifyIcon1.ShowBalloonTip(5, "Connected", "User, Join A Chat Room", ToolTipIcon.Info); ;
            }
            if (i == 2)
            {
                this.notifyIcon1.Icon = new Icon("a3.ico");
                notifyIcon1.ShowBalloonTip(5, "Success", "Voice Chat Ready", ToolTipIcon.Info);
                listView1.Invoke(BoolItemSwitch, new object[] {joinvcStripMenuItem1, true });
                listView1.Invoke(BoolItemSwitch, new object[] { leaveRoomVCToolStripMenuItem, false });
            }
            if (i == 3)
            {
                this.notifyIcon1.Icon = new Icon("a3.ico");
                notifyIcon1.ShowBalloonTip(5, "Failure", "Voice Chat Failed", ToolTipIcon.Info);
                listView1.Invoke(BoolItemSwitch, new object[] { joinvcStripMenuItem1, true });
                listView1.Invoke(BoolItemSwitch, new object[] { leaveRoomVCToolStripMenuItem, false });
            }
        }
        #endregion

        #region ExtenEventshandleers
        private void wd_AddUser(string data)
        {
            listView1.Invoke(addusertolist, new object[] { data });
            
        }

        void wd_VCConnected(string data)
        {
            listView1.Invoke(BoolItemSwitch, new object[] { nicePanel1, true });
            listView1.Invoke(BoolItemSwitch, new object[] { joinvcStripMenuItem1, false });
            listView1.Invoke(BoolItemSwitch, new object[] { leaveRoomVCToolStripMenuItem, true });
            listView1.Invoke(BoolItemSwitch, new object[] { cancelJoinToolStripMenuItem, false });
            listView1.Invoke(BoolItemSwitch, new object[] { ConnTimeoutTimer1, false });
        }

        private void wd_DelUser(string data)
        {
            listView1.Invoke(removeuserfromlist, new object[] { data });
        }

        private void wd_KeyUser(string data)
        {
            xtraProgressBar1.Invoke(currentuserkeyed, new object[] { data });
        }

        private void wd_LevelUser(int data)
        {
            xtraProgressBar1.Invoke(progressbarlevel, new object[] { data });
        }

        void wd_Listners1(string data)
        {
            button1.Invoke(ListenerCount, Convert.ToInt32(data));
        }
        #endregion

        public void JoinVc()
        {
            wd.Connect(true);
            listView1.Invoke(BoolItemSwitch, new object[] { joinvcStripMenuItem1, false });
            listView1.Invoke(BoolItemSwitch, new object[] { leaveRoomVCToolStripMenuItem, false });
            listView1.Invoke(BoolItemSwitch, new object[] { cancelJoinToolStripMenuItem, true });
            listView1.Invoke(BoolItemSwitch, new object[] { ConnTimeoutTimer1, false });
        }

        
        #region Keyhandlers
        private void talkbutton_MouseDown(object sender, MouseEventArgs e)
        {
            xtraProgressBar1.Invoke(progressbarcolor, 0);
            xtraProgressBar1.Invoke(currentuserkeyed, p.username);
            
            button1.BackColor = Color.Red;
            wd.VoiceOut();
        }

        private void talkbutton_MouseUp(object sender, MouseEventArgs e)
        {
            xtraProgressBar1.Invoke(currentuserkeyed, "");
            xtraProgressBar1.Invoke(progressbarlevel, 0);
            xtraProgressBar1.Invoke(progressbarcolor, 1);

            button1.BackColor = Color.Transparent;

            wd.VoiceOff();
        }
        
        private void mutebutton_Click(object sender, EventArgs e)
        {
            VoiceStream.mute = !VoiceStream.mute;
        }
        
        private void joinvcStripMenuItem1_Click(object sender, EventArgs e)
        {
            JoinVc();
        }
        
        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                if(!IgnoreList.Contains(listView1.Items[e.Index].Text))
                ignoreshort(listView1.Items[e.Index].Text);
            }
            else
            {
                if (IgnoreList.Contains(listView1.Items[e.Index].Text))
                unignoreshort(listView1.Items[e.Index].Text);
            }
        }
        
        #region Ignores
        
        private void unignoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            unignoreshort(listView1.SelectedItems[0].Text);
        }
        private void ignoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ignoreshort(listView1.SelectedItems[0].Text);
        }
        private void ignoreshort(string text)
        {
            wd.ignoreuser(text);
            IgnoreList.Add(text);
            listView1.Invoke(checkuserlist, new object[] { text });
        }
        private void ignoreshort(object text)
        {
            string text2 = (string)text;
            wd.ignoreuser(text2);
            IgnoreList.Add(text2);
            listView1.Invoke(checkuserlist, new object[] { text2 });
        }
        private void unignoreshort(string text)
        {
            wd.unignoreuser(text);
            IgnoreList.Remove(text);
            listView1.Invoke(checkuserlist, new object[] { text });
        }
        #endregion
        
        private void leaveRoomVCToolStripMenuItem_Click(object sender, EventArgs e)
        {

                wd.vc_logout();

            //Invoke(notifyiconswitch, new object[] { 1 });
            string fd = p.username;
            p = new YMVCHook.PacketHook();
            p.HookedRoomEvent += new YMVCHook.PacketHook.HookedRoomInfo(p_HookedEvent);
            p.HookedUserNameEvent += new YMVCHook.PacketHook.HookedUserInfo(p_HookedUserNameEvent);
            p.username = fd;
            listView1.Invoke(BoolItemSwitch, new object[] { joinvcStripMenuItem1, true });
            listView1.Invoke(BoolItemSwitch, new object[] { leaveRoomVCToolStripMenuItem, false });
            listView1.Invoke(BoolItemSwitch, new object[] { cancelJoinToolStripMenuItem, false });
            p.start();
            listView1.Invoke(BoolItemSwitch, new object[] { nicePanel1, false });
        }
        private void cancelJoinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            wd.vc_logout();
            listView1.Invoke(BoolItemSwitch, new object[] { joinvcStripMenuItem1, true });
            listView1.Invoke(BoolItemSwitch, new object[] { leaveRoomVCToolStripMenuItem, false });
            listView1.Invoke(BoolItemSwitch, new object[] { cancelJoinToolStripMenuItem, false });
        }
        #endregion
        

        #region Load
        private void Form5_Load(object sender, EventArgs e)
        {
            if (System.IO.File.Exists("ignores.xml"))
            {
                try
                {
                    System.Runtime.Serialization.Formatters.Soap.SoapFormatter s = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
                    System.IO.Stream ss = System.IO.File.Open("ignores.xml", System.IO.FileMode.Open);
                    IgnoreList = (System.Collections.ArrayList)s.Deserialize(ss);
                    ss.Close();
                    s = null;
                    
                }
                catch (Exception)
                {

                }
            } 
            if (IgnoreList == null)
            {
                IgnoreList = new System.Collections.ArrayList();
            }
            Invoke(notifyiconswitch, new object[] { 0 });
        }
        #endregion

        #region Close
        private void closeToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (wd != null)
            {

                wd.Dispose();
                wd = null;
            }
            if (IgnoreList.Count>0)
            {
                System.Runtime.Serialization.Formatters.Soap.SoapFormatter s = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
                System.IO.Stream ss = System.IO.File.Open("ignores.xml", System.IO.FileMode.OpenOrCreate);
                s.Serialize(ss, IgnoreList);
                ss.Flush();
                ss.Close();
                s = null;
                IgnoreList = null;
            }
            Application.Exit();
        }
        #endregion

        

        private void ConnTimeoutTimer1_Tick(object sender, EventArgs e)
        {
            listView1.Invoke(BoolItemSwitch, new object[] { ConnTimeoutTimer1, false });
            wd.vc_logout();
            listView1.Invoke(BoolItemSwitch, new object[] { joinvcStripMenuItem1, true });
            listView1.Invoke(BoolItemSwitch, new object[] { leaveRoomVCToolStripMenuItem, false });
            listView1.Invoke(BoolItemSwitch, new object[] { cancelJoinToolStripMenuItem, false });
            Invoke(notifyiconswitch, new object[] { 3 });
        }

        

       
        
        

        
    }
}
